//
//  HistoryTeamViewController.m
//  Fangmm
//
//  Created by ChaoZhou on 16/3/25.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "HistoryTeamViewController.h"
#import "HttpServer.h"
#import "Constants.h"
#import "Utility.h"
#import <MJRefresh.h>
#import "Team.h"
#import "HistoryTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "Seat.h"
#import "RoomMateListViewController.h"
#import "HouseDetailViewController.h"
@interface HistoryTeamViewController ()<UITableViewDataSource,UITableViewDelegate,HistoryTableViewCellDelegate>
{
    int page;
    NSMutableArray *teamArray;
}
@end

@implementation HistoryTeamViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    page = 2;
    teamArray = [NSMutableArray array];
    self.title = @"历史小组";
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"back-18-36"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(goToMain)];
    self.navigationItem.leftBarButtonItem = leftBar;
    self.mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    self.mainTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.mainTableView.mj_header beginRefreshing];
    [_mainTableView registerNib:[UINib nibWithNibName:@"HistoryTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HistoryTableViewCell"];
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return teamArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 165;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HistoryTableViewCell *historyCell = [tableView dequeueReusableCellWithIdentifier:@"HistoryTableViewCell"];
    historyCell.delegate = self;
    [historyCell configCellWithTeam:teamArray[indexPath.section]];
    return historyCell;
}
-(void)loadMoreData
{
    [HttpServer getRoomMateTeamWithUserId:[Utility getObjectForKey:kUserId] withStatus:nil withPage:page withPageSize:3 withUserInfo:nil withSuccessBlock:^(NSDictionary *responseData)
    {
        
        for (NSDictionary *dic in responseData[@"teams"])
        {
            Team *team = [[Team alloc]initWithDic:dic];
            [teamArray addObject:team];
        }
        [self.mainTableView.mj_header endRefreshing];
        [_mainTableView reloadData];
    }
    withFailedBlock:^(id errorMsg)
    {
        NSLog(@"历史小组%@",errorMsg);
    }];
}
-(void)refreshData
{
    [HttpServer getRoomMateTeamWithUserId:[Utility getObjectForKey:kUserId] withStatus:nil withPage:1 withPageSize:3 withUserInfo:nil withSuccessBlock:^(NSDictionary *responseData)
    {
        [teamArray removeAllObjects];
        NSLog(@"历史小组成功%@",responseData);
        for (NSDictionary *dic in responseData[@"teams"])
        {
            Team *team = [[Team alloc]initWithDic:dic];
            [teamArray addObject:team];
            [self.mainTableView.mj_header endRefreshing];
        }
        [_mainTableView reloadData];
        
    }
    withFailedBlock:^(id errorMsg)
    {
        NSLog(@"历史小组%@",errorMsg);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)queryRoommateWithSender:(UIButton *)sender
{
    NSLog(@"查询室友");
    RoomMateListViewController *roomMateListVC =[[RoomMateListViewController alloc]init];
    roomMateListVC.team = (Team*)(teamArray[sender.tag]);
    [self.navigationController pushViewController:roomMateListVC animated:YES];
}
-(void)goToMain
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HouseDetailViewController *houseDetailVC = [[HouseDetailViewController alloc]init];
    houseDetailVC.houseId = ((Team*)teamArray[indexPath.section]).houseId;
    [self.navigationController pushViewController:houseDetailVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
